<?php

class Reports_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getUrl() {
        $this->db->select('*');
        $this->db->from('track_url');
        $this->db->order_by("id", "DESC");
        $query = $this->db->get();
        return $query->result();
    }

    public function record_count() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        $this->db->where('url_owner', $id);
        $this->db->from('track_url');
        return $this->db->count_all_results();
    }

    public function fetch_urls($limit, $start) {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;

        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('track_url');
        $this->db->where('url_owner', $id);
        $this->db->order_by("id", "DESC");
        $query = $this->db->get();
        // echo $this->db->last_query();
        return $query->result();
    }

    public function searchUrls($limit, $start) {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;

        $this->db->limit($limit, $start);
        $this->load->library('session');

        if ($_POST) {
            $this->session->set_userdata($_POST);
        }
        $data = $this->session->all_userdata();
        if (isset($data['long_url'])) {
            $long_url = $data['long_url'];

            $short_url = substr($data['short_url'], -6);

            $datepickerstart = $data['create_timestart'];
            $datepickerend = $data['create_timeend'];
            $os = $data['os'];
            $browser = $data['browser'];

            //$long_url = $this->input->post('long_url');
            //$this->session->set_userdata('url',$long_url);
            // $long_url = $this->input->post('long_url');
            // $short_url = $this->input->post('short_url');

            $datepickerstart = date("Y-m-d", strtotime($data['create_timestart']));
            $datepickerend = date("Y-m-d", strtotime($data['create_timeend'])) . '23:59:59';

            if ($os != '') {
                $this->db->like('os', $os);
            }
            if ($browser != '') {
                $this->db->like('browser', $browser);
            }
            if ($long_url != '') {
                $this->db->like('long_url', $long_url);
            }
            if ($short_url != '') {
                $this->db->like('short_url', $short_url);
            }
            if ($datepickerstart != '1970-01-01') {
                if ($datepickerstart != $datepickerend) {
                    $this->db->where('hit_time >=', $datepickerstart);
                    $this->db->where('hit_time <=', $datepickerend);
                } else
                    $this->db->like('hit_time', $datepickerstart);
            }
        }
        if (isset($_GET['short'])) {
            $this->session->set_userdata($_GET);
            $data = $this->session->all_userdata();
            //print_r($data); die;
        }
        if (isset($data['short'])) {
            $this->db->like('short_url', $data['short']);
        }
        $this->db->select('*');
        $this->db->from('track_url');
        $this->db->where('url_owner', $id);
        $this->db->order_by("id", "DESC");
        $query = $this->db->get();
        //echo $this -> db -> last_query();
        return $query->result();
    }

    public function searchUrls1() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;

        $this->load->library('session');

        if ($_POST) {
            $this->session->set_userdata($_POST);
        }

        $data = $this->session->all_userdata();
        if (isset($data['long_url'])) {
            $long_url = $data['long_url'];
            $short_url = substr($data['short_url'], -6);
            $datepickerstart = $data['create_timestart'];
            $datepickerend = $data['create_timeend'];
            $os = $data['os'];
            $browser = $data['browser'];

            //$long_url = $this->input->post('long_url');
            //$this->session->set_userdata('url',$long_url);
            // $long_url = $this->input->post('long_url');
            // $short_url = $this->input->post('short_url');

            $datepickerstart = date("Y-m-d", strtotime($data['create_timestart']));
            $datepickerend = date("Y-m-d", strtotime($data['create_timeend'])) . '23:59:59';

            if ($os != '') {
                $this->db->like('os', $os);
            }
            if ($browser != '') {
                $this->db->like('browser', $browser);
            }
            if ($long_url != '') {
                $this->db->like('long_url', $long_url);
            }
            if ($short_url != '') {
                $this->db->like('short_url', $short_url);
            }
            if ($datepickerstart != '1970-01-01') {
                if ($datepickerstart != $datepickerend) {
                    $this->db->where('hit_time >=', $datepickerstart);
                    $this->db->where('hit_time <=', $datepickerend);
                } else
                    $this->db->like('hit_time', $datepickerstart);
            }
        }
        if (isset($_GET['short'])) {
            $this->session->set_userdata($_GET);
            $data = $this->session->all_userdata();
            //print_r($data); die;
        }
        if (isset($data['short'])) {
            $this->db->like('short_url', $data['short']);
        }
        $this->db->select('*');
        $this->db->from('track_url');
        $this->db->where('url_owner', $id);
        $this->db->order_by("id", "DESC");

        $query = $this->db->get();
        //echo $this -> db -> last_query();
        return $query->result();
    }

    function DeleteUrl($id) {

        $id = $_GET['id'];
        $this->db->delete('track_url', array('id' => $id));
        return $this->db->affected_rows();
        // $location = 'location:' . base_url() . "reports?msg=SuccessFully Deleted id $id";
        //header($location);
    }

    public function getbrowser() {
        $this->db->select('browser');
        $this->db->from('track_url');
        $this->db->group_by('browser');
        $query = $this->db->get();
        return $query->result();
    }

    public function getos() {
        $this->db->select('os');
        $this->db->from('track_url');
        $this->db->group_by('os');
        $query = $this->db->get();
        return $query->result();
    }

    function ipdetail() {
        $ip = $_GET['id'];
        $this->db->select('*');
        $this->db->from('ip_detail');
        $this->db->where('ip', $ip);
        $query = $this->db->get();
        return $query->result();
    }

}

?>
